rm(list = ls())
# Function to load packages
loadPkg=function(toLoad){
       for(lib in toLoad){
              if(! lib %in% installed.packages()[,1])
              {install.packages(lib, repos='http://cran.rstudio.com/')}
              suppressMessages( library(lib, character.only=TRUE))}}

# Load libraries
packs=c('rgeos','rgdal','sf','maptools','ggplot2','countrycode','dplyr','scales',
        'ggmap','ggthemes','cshapes','readstata13','arm','viridis',
        'ggridges','purrr','dplyr',"haven","readxl","sbgcop")
loadPkg(packs)

### Figure -1
load("bri.RData")

bri <- bri %>% 
  dplyr::mutate(join14 = ifelse(year == 2014, 1, 0),
                join15 = ifelse(year <= 2015, 1, 0),
                join16 = ifelse(year <= 2016, 1, 0),
                join17 = ifelse(year <= 2017, 1, 0),
                join18 = ifelse(year <= 2018, 1, 0),
                join19 = ifelse(year <= 2020, 1, 0)) #	Kiribati joined in 2020
                
        

#get a shaplefile as of 2014-12031
world <- cshp(date = as.Date("2015-12-31"),useGW = F)
# fortify for mapping
world_shp <- fortify(world , region = "ISO1AL3")
# add country info and mergy by ccode
world_shp <- left_join(world_shp, bri, by = c("id" = "iso"))
#creat a group variable; countries not jointed as 0, and 1= already joined
world_shp <- world_shp %>% 
       dplyr::mutate(join14 = ifelse(is.na(join14) & id != "TWN" & id != "CHN" , 0, join14),
                     join15 = ifelse(is.na(join15) & id != "TWN" & id != "CHN" , 0, join15),
                     join16 = ifelse(is.na(join16) & id != "TWN" & id != "CHN" , 0, join16),
                     join17 = ifelse(is.na(join17) & id != "TWN" & id != "CHN" , 0, join17),
                     join18 = ifelse(is.na(join18) & id != "TWN" & id != "CHN" , 0, join18),
                     join19 = ifelse(is.na(join19) & id != "TWN" & id != "CHN" , 0, join19)) %>% 
       dplyr::mutate_at(vars(join14, join15, join16, join17, join18, join19), as.factor)
       
# load font for chinese
library(extrafont)
library(showtext)
showtext_auto(enable = TRUE)
#font_add('SimSun', 'SimSun.ttf')
#font_add('KaiTi', 'KaiTi.ttf')
library(tidyr)

map <- gather(world_shp, key = "yearjoin", join, join14:join19)

map$yearjoin <- factor(map$yearjoin, labels = c("2014年", "2015年", "2016年", "2017年", "2018年", "2019年"))

# make a map
ggplot() + 
       geom_polygon(data = map, 
                    aes(x = long, y = lat, group = group, 
                        fill =  join) , 
                    size = 0.25) + 
       coord_fixed() +
       scale_fill_manual(values = c("0" = "gray",
                                    "1" = "blue"),
                         na.value = "grey50")+
       theme_bw() + facet_wrap(~yearjoin, nrow = 3)+
       theme(legend.position = "none",
               line = element_blank(),
             rect = element_blank(), #defien the margin line
             axis.text = element_blank(),
             axis.title = element_blank(), 
             panel.background = element_blank(), 
             panel.grid.major = element_blank(), 
             panel.grid.minor = element_blank(),
             axis.ticks.length = unit(0,"cm"),
             panel.spacing = unit(0, "lines"), 
             text = element_text(size=16, family = 'KaiTi'),
             plot.margin = margin(-4, 0, -4, 0, "cm")) #top, right, bottom, left.
ggsave("Figure1.jpeg", width = 6, height = 8)



